
/**
 * Wrapper event callback
 * @param  {[type]}   event [description]
 * @param  {Function} cb    [description]
 * @return {[type]}         [description]
 */
function callbackEvent(event, cb) {
  return typeof cb === 'function'
    ? (e, ...argv) => {
      if (e && typeof e[event] === 'function') {
        e[event]();
      }
      return cb(e, ...argv);
    }
    : undefined;
}

/**
 * stopPropagation - Wrapper preventDefault callback
 * @param  {Function} cb [description]
 * @return {[type]}      [description]
 */
export function stopPropagation(cb) {
  return callbackEvent('stopPropagation', cb);
}

/**
 * preventDefault - Wrapper preventDefault callback
 * @param  {Function} cb [description]
 * @return {[type]}      [description]
 */
export function preventDefault(cb) {
  return callbackEvent('preventDefault', cb);
}

/**
 * window.scrollTo
 * @param  {[type]} x [description]
 * @param  {[type]} y [description]
 * @return {[type]}   [description]
 */
export function scrollTo(x, y) {
  window.scrollTo(x, y);
}

/**
 * addEventListener
 * @param {[type]} element [description]
 * @param {[type]} event   [description]
 * @param {[type]} handler [description]
 */
export function addEventListener(element, event, handler) {
  const addEvent = element.attachEvent || element.addEventListener;
  const e = element.attachEvent ? `on${event}` : event;
  addEvent.apply(element, [e, handler, true]);
}

/**
 * removeEventListener
 * @param  {[type]} element [description]
 * @param  {[type]} event   [description]
 * @param  {[type]} handler [description]
 * @return {[type]}         [description]
 */
export function removeEventListener(element, event, handler) {
  const removeEvent = element.detachEvent || element.removeEventListener;
  const e = element.detachEvent ? `on${event}` : event;
  removeEvent.apply(element, [e, handler]);
}
